class Trie {
public:
    Trie() {}
    
    void insert(string word) {
        Trie* curr = this;
        for (int i = 0; i < word.size(); i++)
        {
            int index = word[i] - 'a';
            if (curr->next[index] == nullptr)
            {
                Trie* newNode = new Trie;
                curr->next[index] = newNode;
            }
            curr = curr->next[index];
        }
        curr->isEnd = true;
    }
    
    bool search(string word) {
        Trie* curr = this;
        for (int i = 0; i < word.size(); i++)
        {
            int index = word[i] - 'a';
            if (curr->next[index] == nullptr)
                return false;
            curr = curr->next[index];
        }
        return curr->isEnd;     //为真，就说明word的结束字符在字典树中也被标记为结束
    }
    
    bool startsWith(string prefix) {
        Trie* curr = this;
        for (int i = 0; i < prefix.size(); i++)
        {
            int index = prefix[i] - 'a';
            if (curr->next[index] == nullptr)
                return false;
            curr = curr->next[index];
        }
        return true;
    }
private:
    bool isEnd = false;             //该字符是否为结尾
    Trie* next[26] = {nullptr};
};

/**
 * Your Trie object will be instantiated and called as such:
 * Trie* obj = new Trie();
 * obj->insert(word);
 * bool param_2 = obj->search(word);
 * bool param_3 = obj->startsWith(prefix);
 */